package indi.caibo.service_ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import indi.caibo.commonutils.JwtUtils;
import indi.caibo.commonutils.MD5;
import indi.caibo.service_ucenter.entity.UcenterMember;
import indi.caibo.service_ucenter.entity.vo.backend.LoginVo;
import indi.caibo.service_ucenter.entity.vo.backend.RegisterVo;
import indi.caibo.service_ucenter.mapper.UcenterMemberMapper;
import indi.caibo.service_ucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import indi.caibo.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Bo Cai
 * @since 2021-12-01
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        String mail = loginVo.getMail();
        String password = loginVo.getPassword();
        /*参数校验*/
        if (StringUtils.isEmpty(mail) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败：参数错误");
        }
        /*获取会员*/
        UcenterMember member = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mail", mail));
        if (null == member) {
            throw new GuliException(20001, "登录失败：无法查询到该用户");
        }
        /*检验密码*/
        if (!MD5.encrypt(password).equals(member.getPassword())) {
            throw new GuliException(20001, "登录失败：密码错误");
        }
        /*校验该账号是否被禁用*/
        if (member.getIsDisabled()) {
            throw new GuliException(20001, "登录失败：该账户已被禁用");
        }
        //使用JWT生成token字符串
        return JwtUtils.getJwtToken(member.getId(), member.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        /*获取注册信息，用于后续校验*/
        String mail = registerVo.getMail();
        String nickname = registerVo.getNickname();
        String code = registerVo.getCode();
        String password = registerVo.getPassword();
        /*参数校验*/
        if (StringUtils.isEmpty(mail) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "注册失败：参数错误");
        }
        /*验证码校验*/
        /*从Redis获取验证码进行验证*/
        if (!code.equals(redisTemplate.opsForValue().get(mail))) {
            throw new GuliException(20001, "注册失败：验证码错误");
        }
        /*邮箱重复验证*/
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mail", mail));
        if (count > 0) {
            throw new GuliException(20001, "注册失败：邮箱重复");
        }
        /*添加注册信息到数据库*/
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMail(mail);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        this.save(member);
    }

    @Override
    public UcenterMember getLoginInfo(String memberId) {
        return baseMapper.selectById(memberId);
    }

    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.countRegisterByDay(day);
    }
}
